package com.nile;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.sql.Time;
import java.time.DateTimeException;
import java.time.Duration;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: zhang kai
 * @Date: 2022/08/04
 * @Description:
 */
public class Demo {

    public static void main(String[] args) throws Exception{
        System.out.println(Integer.parseInt("00") == 0);
        System.out.println(LocalTime.MIDNIGHT.compareTo(LocalTime.MIN));
        LocalTime s1 = LocalTime.parse("18:30:00");
        LocalTime e1 = LocalTime.parse("18:50:00");
        LocalTime s2 = LocalTime.parse("00:00:00");
        LocalTime e2 = LocalTime.parse("00:00:00");
//        if (s1.compareTo(e1) >= 0) {
//            System.out.println(1111);
//        }

        System.out.println(isOverlap(s1,e1,s2,e2,true));

//        List<Rule> rules = new ArrayList<>();
//        rules.add(new Rule(Time.valueOf("8:30:00"), Time.valueOf("9:30:00")));
//        rules.add(new Rule(Time.valueOf("14:00:00"), Time.valueOf("14:30:00")));
//        rules.add(new Rule(Time.valueOf("15:00:00"), Time.valueOf("15:30:00")));
//        rules.add(new Rule(Time.valueOf("16:00:00"), Time.valueOf("16:30:00")));
//        validRuleParam("13:00","19:00",rules);

    }

    private static Boolean validRuleParam(String s1, String s2, List<Rule> rules) throws Exception {
        //判断时间格式
        String[] s = s1.split(":");
        String[] e = s2.split(":");
        if (s.length != 2 || e.length != 2) {
            throw new Exception("时间格式不正确！");
        }
        //判断是否超过24小时
        if (Integer.parseInt(s[0]) > 23 || Integer.parseInt(e[0]) > 23 ) {
            throw new Exception( "24小时制时间格式不正确！");
        }
        LocalTime l1 = LocalTime.of(Integer.parseInt(s[0]), Integer.parseInt(s[1]));
        LocalTime l2 = LocalTime.of(Integer.parseInt(e[0]), Integer.parseInt(e[1]));
        //开始时间不能大于结束时间
        if (l1.isAfter(l2)) {
            throw new Exception( "开始时间大于结束时间！");
        }
        //是否是整点半点
        if (l1.getMinute() != 0 && l1.getMinute() != 30) {
            throw new Exception( "时间不是整点或者半点！");
        }
        if (l2.getMinute() != 0 && l2.getMinute() != 30) {
            throw new Exception( "时间不是整点或者半点！");
        }
        //判断起止时间是否间隔30分钟
        Duration duration = Duration.between(l1, l2);
        if (duration.toMinutes() % 30 != 0) {
            throw new Exception( "起止时间间隔至少30分钟！");
        }

        // -----
        //   -----if(! (endDate1<=startDate2 || startDate1>=endDate2)){
        //            return true;
        //        }
        //判断时间段是否重复
        for (Rule po : rules) {
            LocalTime r1 = po.getStartTime().toLocalTime();
            LocalTime r2 = po.getEndTime().toLocalTime();
            //buchongdie
            if (!(l1.compareTo(r2) >=0 || l2.compareTo(r1) <=0)){
                throw new Exception("error");
            }
        }

        return true;
    }

    /**
     * @param s1
     * @param e1
     * @param s2
     * @param e2
     * @param isStrict 是否严格遵守不能重叠，例如如果为true 那么8：00-8：30 和8：30-9：00 时间段比较为true
     * @return true重叠。false不重叠
     */
    public static boolean isOverlap(LocalTime s1, LocalTime e1, LocalTime s2, LocalTime e2, boolean isStrict) {
        if (s1.isAfter(e1) || s2.isAfter(e2)) {
            throw new DateTimeException("endDate不能小于startDate");
        }
        if (isStrict) {
            if (!(s1.compareTo(e2) >0 || e1.compareTo(s2) < 0)) {
                return true;
            }
        } else {
            if (!(s1.compareTo(e2) >=0 || e1.compareTo(s2) <= 0)) {
                return true;
            }
        }
        return false;
    }

}
@Data
@AllArgsConstructor
class Rule {
    private Time startTime;
    private Time endTime;
}
